import { Injectable } from '@angular/core';
import { I18NService } from '../core/i18n/api';
import { ModalService } from '../modal/modal.service';
import { LvConfig } from '../core/config';
import { ModalRef } from '../modal/modalRef';

@Injectable({ providedIn: 'root' })
export class MessageboxService {
  iconMap: any = {
    confirm: 'lv-icon-popup-question-48',
    success: 'lv-icon-status-succeed',
    info: 'lv-icon-status-info',
    warning: 'lv-icon-status-warning-info',
    error: 'lv-icon-status-error',
    danger: 'lv-icon-status-danger',
  };

  titleMap: any = {
    confirm: this.i18n.get('msgBoxConfirm'),
    info: this.i18n.get('msgBoxInfo'),
    success: this.i18n.get('msgBoxSuccess'),
    error: this.i18n.get('msgBoxError'),
    warning: this.i18n.get('msgBoxWarning'),
    danger: this.i18n.get('msgBoxDanger'),
  };

  constructor(
    private modalService: ModalService,
    private i18n: I18NService,
  ) {}

  info(options): ModalRef<any> {
    return this._create(options, 'info');
  }

  error(options): ModalRef<any> {
    return this._create(options, 'error');
  }

  danger(options): ModalRef<any> {
    return this._create(options, 'danger');
  }

  confirm(options): ModalRef<any> {
    return this._create(options, 'confirm');
  }

  success(options): ModalRef<any> {
    return this._create(options, 'success');
  }

  warning(options): ModalRef<any> {
    return this._create(options, 'warning');
  }

  formatOptions(_options): any {
    return {
      ...{
        lvWidth: 360,
        lvKeyboardEscape: false,
        lvOuterClosable: false,
        lvMaskTransparent: false,
      },
      ...LvConfig.messageBoxOptions,
      ..._options,
    };
  }

  private _create(options, dialogType): ModalRef<any> {
    const { titleMap, iconMap } = this;

    let _options: {
      lvType: string;
      lvVisible: boolean;
      lvModality: boolean;
      lvContent?: any;
      lvHeader?: any;
      lvDialogIcon?: any;
      lvCancelText?: string;
      lvOkType?: string;
      lvClass?: string;
    } = {
      lvType: 'dialog',
      lvVisible: false,
      lvModality: true,
      lvClass: 'lv-message-box',
    };
    if (typeof options === 'string') {
      _options.lvContent = options;
      _options.lvHeader = titleMap[dialogType];
      _options.lvDialogIcon = iconMap[dialogType];
      if (dialogType !== 'confirm') {
        // Remove the Cancel button if dialog is not confirm
        _options.lvCancelText = null;
        _options.lvOkType = 'default';
      }
    } else if (typeof options === 'object') {
      if (!('lvHeader' in options)) {
        options.lvHeader = titleMap[dialogType];
      }
      if (!('lvDialogIcon' in options)) {
        options.lvDialogIcon = iconMap[dialogType];
      }
      if (dialogType !== 'confirm') {
        // Remove the Cancel button if dialog is not confirm
        options.lvCancelText = null;

        if (!('lvOkType' in options)) {
          options.lvOkType = 'default';
        }
      }
      if (!options.lvDialogIcon) {
        _options.lvClass = `${_options.lvClass} lv-message-box-no-icon`;
      }
      if ('lvClass' in options) {
        _options.lvClass = `${_options.lvClass} ${options.lvClass}`;
      }

      _options = { ...options, ..._options };
    }

    _options = this.formatOptions(_options);

    return this.modalService.create(_options);
  }
}
